<?php
namespace app\admin\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Request;
use think\captcha\facade\Captcha;

class Adm extends BaseController
{
    protected $data = [];
    protected $code = 1;
    protected $msg = 'ok';
    // 初始化
    protected function initialize()
    {
        $this->data = Request::param();
        \think\facade\Config::load('extra/config', 'extra');
        $auth = \app\admin\auth\Auth::check();
        $this->setmodel();
        if(!$auth)
        {
        //    echo 'error';
        //    $error = \app\admin\auth\Auth::error()[0];
        //    $this->fail($error);
        }
    }
    protected function fetch($tmp=null)
    {
        $this->response['code'] = $this->code;
        $this->response['msg'] = $this->msg;
        //自动处理返回数据格式
        if(Request::isAjax()){
            //return $this->response;
            return json($this->response);
        }
        View::assign($this->response);
        if($tmp)
        {
            return View::fetch($tmp);
        }else{
            return View::fetch();
        }
    }
    protected function setmodel()
    {

    }
    protected function strtoarr($str)
    {
        //echo ord('A').'-'.ord('Z');
        $str = 'AbcDse';
        $len = strlen($str);
        $substr = $str;
        $subarr = [];
        $sub_start = 0;
        $sub_end = 0;
        for($i=0;$i<$len;$i++){
            if(ord($str[$i])>64 && ord($str[$i])<91){
                $sstr=substr($str,$sub_start,$i-$sub_start);
                if($sstr!=''){
                    $subarr[] = strtolower($sstr);
                }
                $substr = substr($substr,$i);
                $sub_start = $i;
            }   
        }
        if($substr!=''){
            $subarr[] = strtolower($substr);
        }
        return $subarr;
    }

    /**
    * 通用上传
    */
    protected function saveFile($file,$path)
    {
        // 上传到本地服务器
        
        try {
        validate(['file' => [
            'fileSize' => 1048576,
            'fileExt' => 'jpg,png,gif,jpeg',
            'fileMime' => 'image/jpeg,image/png,image/gif',
        ]])->check(['file' => $file]);
        $savename = \think\facade\Filesystem::disk('public')->putFile($path, $file);
        if (!$savename) {
            return json(['', 'code' => 0, 'msg' => '上传失败']);
        }
        return json(['savename' => '/uploads/'.$savename, 'code' => 1, 'msg' => '上传成功']);
        } catch (ValidateException $e) {
            return $e->getMessage();
        }
    }
}
